package com.tanhua.server.service;

import com.mongodb.Mongo;
import com.tanhua.autoconfig.templates.HuanXinTemplate;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.MessageLikeVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentsService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity queryComments(String movementId, Integer page, Integer pagesize) {
//        Mongo
       PageResult pageResult = commentApi.queryComments(movementId,page,pagesize);
       List<Comment> commentList = (List<Comment>) pageResult.getItems();
       List<CommentVo> list = new ArrayList<>();
//        mysql

        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            CommentVo commentVo = new CommentVo();
            commentVo.setId(comment.getId().toString()); //评论
            commentVo.setAvatar(userInfo.getAvatar());
            commentVo.setNickname(userInfo.getNickname());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new SimpleDateFormat("HH:mm").format( new Date(comment.getCreated()))); //创建时间  HH:mm
            commentVo.setLikeCount(comment.getLikeCount()); //这个评论收到的点赞数

            if(redisTemplate.hasKey("comment_like_"+comment.getId().toString()+"_"+ UserHolder.getUserId())){
                commentVo.setHasLiked(1); //当前登录人对这个评论是否点赞
            }else{
                commentVo.setHasLiked(0); //当前登录人对这个评论是否点赞
            }
            list.add(commentVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);


    }

    public ResponseEntity saveComments(String movementId, String commentContent) {
//        1、向Comment添加一条数据   2、修改Movement的评论数 +1
        Comment comment = new Comment();
//        comment.setId(new ObjectId());  在dubbo那边赋值
        comment.setPublishId(new ObjectId(movementId)); //有可能是圈子ID、小视频id 、评论id
        comment.setCommentType(2);   //评论类型，1-动态点赞，2-动态评论，3-动态喜欢 4-视频点赞  5-视频评论  6-评论点赞
        comment.setContent(commentContent); //评论的内容
        comment.setUserId(UserHolder.getUserId()); //
//        comment.setLikeCount(0); //当前评论收到的点赞数据
//        comment.setCreated();  在dubbo那边赋值

//        comment.setPublishUserId(movement.getUserId()); //圈子的作者   在dubbo那边赋值

        Integer likeCount = commentApi.saveComment(comment);

        return ResponseEntity.ok(likeCount);

    }

    public ResponseEntity queryLikesOrLovesOrComments(Integer page, Integer pagesize , Integer commentType) {
//        查询的是当前登录人的圈子收到的点赞数据  select * from  comment where  publishUserId=1  and commentType=1
       PageResult pageResult =  commentApi.queryLikesOrLovesOrComments(UserHolder.getUserId(),page,pagesize,commentType);
        List<Comment> commentList = (List<Comment>) pageResult.getItems();

        List<MessageLikeVo> list = new ArrayList<>();
        for (Comment comment : commentList) {
             UserInfo userInfo =  userInfoApi.findById(comment.getUserId());
            MessageLikeVo messageLikeVo = new MessageLikeVo();
            messageLikeVo.setId(userInfo.getId().toString());
            messageLikeVo.setAvatar(userInfo.getAvatar());
            messageLikeVo.setNickname(userInfo.getNickname());
            messageLikeVo.setCreateDate( new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated()))  ); //

            list.add(messageLikeVo);
        }

        pageResult.setItems(list);
        return ResponseEntity.ok(pageResult);

    }

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private FriendApi friendApi;

    public ResponseEntity beFriends(Long friendId) {
//        1、向mongo中插入数据
        friendApi.save(UserHolder.getUserId(),friendId);
//        2、环信上成为好友
        huanXinTemplate.contactUsers(UserHolder.getUserId(),friendId);

        return ResponseEntity.ok(null);

    }
}
